import {HttpParams} from '@angular/common/http';
import {Dictionary} from './model/share.model';
import {PageConfig} from './model/page-config.model';

/**
 * 将两个对象合并成一个对象
 * @param oldData : 原始对象
 * @param newData : 新对象
 * @returns {any} : 返回合并的对象
 * @constructor
 */
export function ObjectCopy(oldData: any, newData: any) {
  if (!oldData) {
    oldData = {};
  }
  if (!newData) {
    newData = {};
  }
  Object.keys(newData).forEach(field => {
    oldData[field] = newData[field];
  });
  return oldData;
}

export function Clone(data?: any): any {
  let res = {};
  if (data) {
    Object.keys(data).forEach(key => {
      res[key] = data[key];
    });
  }
  return res;
}

/**
 * 处理查询区域中的参数信息，将对象转成查询字符串
 * @param {any} model
 * @param {any} form
 * @returns {any}
 * @constructor
 */
export function  HandleParamsString(model, oldParams?): any {
  let queryString = '';
  Object.keys(model).forEach(field => {
    if ( model[field] ) {
      queryString += queryString.length > 0
        ? `&${field}=${model[field]}`
        : `${field}=${model[field]}`;
    }
  });
  Object.keys(oldParams).forEach(field => {
    if ( oldParams[field] ) {
      queryString += queryString.length > 0
        ? `&${field}=${oldParams[field]}`
        : `${field}=${oldParams[field]}`;
    }
  });
  const params = new HttpParams({fromString: queryString});
  return params;
}

export function  HandleParams(model, oldParams?): any {
  const res = oldParams;
  Object.keys(model).forEach(field => {
    if (model[field]) {
      oldParams[field] = model[field];
    }
  });
  const params = new HttpParams({fromObject: res});
  return params;
}

export function  HandleDictionaryMap(data) {
  const pageDic = new Map();
  if (data == null) {
    return;
  }
  if (data.dicconfiglist) {
    data.dicconfiglist.forEach(item => {
      const valueItem: Dictionary[] = [];
      if (item.isDictionaryData && data.diclist != null) {
        const idiclist = data.diclist.filter(m => m.useForField === item.fieldName);
        if (idiclist.length > 0) {
          idiclist.forEach(i => valueItem.push(i));
        }
      }
      if (!item.isDictionaryData && data.otherdiclist != null) {
        // console.log(data.otherdiclist);
        const iotherdiclist = data.otherdiclist.filter(m => m.useForField === item.fieldName);
        if (iotherdiclist.length > 0) {
          iotherdiclist.forEach(i => valueItem.push(i));
        }
      }
      pageDic.set(item.fieldName, valueItem);
    });
  }
  return pageDic;
}

export function  HandleDictionaryObj(data) {
  const pageDic = {};
  if (data == null) {
    return;
  }
  if (data.dicconfiglist) {
    data.dicconfiglist.forEach(item => {
      const valueItem: Dictionary[] = [];
      if (item.isDictionaryData && data.diclist != null) {
        const idiclist = data.diclist.filter(m => m.useForField === item.fieldName);
        if (idiclist.length > 0) {
          idiclist.forEach(i => valueItem.push(i));
        }
      }
      if (!item.isDictionaryData && data.otherdiclist != null) {
        // console.log(data.otherdiclist);
        const iotherdiclist = data.otherdiclist.filter(m => m.useForField === item.fieldName);
        if (iotherdiclist.length > 0) {
          iotherdiclist.forEach(i => valueItem.push(i));
        }
      }
      pageDic[item.fieldName] = valueItem;
    });
  }
  return pageDic;
}

/**
 * 处理表单配置信息
 * @param data
 * @returns {PageConfig}
 */
export function  handleDialogConfig(data): PageConfig {
  data.formErrors = {};
  data.formErrorKeys = [];
  data.notInFormErrorKey = [];
  data.vMessages = {};
  const fields = data.fields;

  if (data.fields) {
    data.property = [];
    fields.forEach(field => {
      data.property[field.name] = field;
    });
  }
  for (const item in data.property) {
    const pro = data.property[item];
    if (pro['validates'] && pro['validates'].length > 0) {
      data.formErrors[item] = '';
      data.formErrorKeys.push(item);
      data.vMessages[item] = [];
      pro['validates'].forEach(val => {
        data.vMessages[item][val] = `[${[pro.title]}] ${this.config.validateMessages[val]}`;
      });
    }
    if (pro['notInForm']) {  // 不在form表单中的控件,即自己写的组件
      data.notInFormErrorKey.push(item);
    }
  }
  return data;
}
